package com.hellpro.lolbox.acticity.view;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;

import butterknife.BindView;

public class NormalActivity extends LolBaseActivity {


    @BindView(R.id.main_normal_return)
    ImageView mainNormalReturn;
    @BindView(R.id.main_normal_btn_fabu)
    Button mainNormalBtnFabu;
    @BindView(R.id.main_et_text)
    EditText mainEtText;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.tv_1)
    TextView tv1;
    @BindView(R.id.iv)
    ImageView iv;
    @BindView(R.id.tv_pengy)
    TextView tvPengy;
    @BindView(R.id.activity_normal)
    LinearLayout activityNormal;

    @Override
    public int getLayoutId() {
        return R.layout.activity_normal;
    }

    @Override
    public void initView() {
        mainEtText.addTextChangedListener(mTextWatcher);
        mainNormalReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(NormalActivity.this);
                builder.setTitle("提示").setMessage("真的要退出吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("取消", null).show();
            }
        });

        mainNormalBtnFabu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NormalActivity.this, "正在完善", Toast.LENGTH_SHORT).show();
            }
        });
    }

    //设置EditText的监听
    TextWatcher mTextWatcher = new TextWatcher() {
        private CharSequence temp;
        private int editStart;
        private int editEnd;

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            temp = s;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
//          mTextView.setText(s);//将输入的内容实时显示
        }

        /**
         *
         * 类描述:
         * 创建人:阿犇
         * 创建时间:2016/10/21
         * 备注:设置输入字体的字数
         */
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            editStart = mainEtText.getSelectionStart();
            editEnd = mainEtText.getSelectionEnd();
            tv1.setText(String.valueOf(temp.length()));
            if (temp.length() > 140) {
                Toast.makeText(NormalActivity.this,
                        "你输入的字数已经超过了限制！", Toast.LENGTH_SHORT)
                        .show();
                s.delete(editStart - 1, editEnd);
                int tempSelection = editStart;
                mainEtText.setText(s);
                mainEtText.setSelection(tempSelection);
            }
        }
    };

    /**
     *
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/21
     * 备注:处理返回键
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("提示").setMessage("真的要退出吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("取消", null).show();
                break;
        }

        return false;
    }

}
